package ModelLayer;
import java.util.HashMap; 
import java.util.HashSet;
import java.util.ArrayList; 
import java.util.Iterator;

/**
 * Write a description of class StaffMemberCollection here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CustomerCollection
{

       private static HashMap<String, Group> customer_groups;
       private static ArrayList<Customer> customers;
       private static FileCustomer file;
       private static FileGroup groupfile;
       private static CustomerCollection instance;
       
    /**
     * Constructor for objects of class StaffMemberCollection
     */
    private CustomerCollection()
    {
        customers = new ArrayList<>();
        customer_groups = new HashMap<>();
        file = new FileCustomer();
        groupfile = new FileGroup();
        customers = file.get_data();
        customer_groups = groupfile.get_data();
    }
    
    public static void initialize()
    {
        if(instance == null)
        {
            instance = new CustomerCollection();
        }
    }
    
    public static ArrayList<Customer> get_Customers()
    {
        return customers;
    }
    
    public static HashMap<String, Group> get_customer_groups()
    {
        return customer_groups;
    }
    
    public static CustomerCollection get_instance()
    {
        return instance;
    }
    
    public static String add_customer(String name, String phone_nr, String address, String postal_code, String city, String group)
    {
        ArrayList<String> lease_ids = new ArrayList<>();
        ArrayList<String> order_ids = new ArrayList<>();
        int max_temp_id = 0;
        int temp_id = 0;
        int possibly_not_taken = 1;
        String id;
        for (Customer member : customers)
        {
            temp_id = Integer.parseInt(member.get_id());
            if(max_temp_id < temp_id)
            {
                if(max_temp_id < (temp_id - 1))
                {
                    if(!(existing_customer(id = "" + (max_temp_id + 1))))
                    {
                        break;
                    }
                }
                max_temp_id = temp_id;     
            }
        }
        id = "" + (max_temp_id + 1);
        customers.add(new Customer(id, name, phone_nr, address, postal_code, city, group, lease_ids, order_ids));
        boolean temp = file.save_changes(customers);
        return id;
    }
    
    public static boolean existing_customer(String id)
    {
        for (Customer member : customers)
        {
            if(member.get_id().equals(id))
            {
                return true;
            }
        }
        return false; 
    }
    
    public static Customer find_customer(String id)
    {
        for (Customer member : customers)
        {
            if(member.get_id().equals(id))
            {
                return member;
            }
        }
        return null;
    }
    
    public static boolean delete_customer(String id)
    {
        return customers.remove(find_customer(id));
    }
    
    public static boolean update_customer(String id, String name, String phone_nr, String address, String postal_code, String city, String group)
    {
       if (name != "")
       {
           find_customer(id).set_name(name);
       }
       if (phone_nr != "")
       {
           find_customer(id).set_phone_nr(phone_nr);
       }
       if (address != "")
       {
           find_customer(id).set_address(address);
       }
       if (postal_code != "")
       {
           find_customer(id).set_postal_code(postal_code);
       }
       if (city != "")
       {
           find_customer(id).set_city(city);
       }
       if (group != "")
       {
           find_customer(id).set_group(group);
       }
       return file.save_changes(customers);
    }
    
    public static void add_order_to_customer(String id, String order_id)
    {
        find_customer(id).add_order(order_id);
    }
    
    public static void add_lease_to_customer(String id, String lease_id)
    {
        find_customer(id).add_lease(lease_id);
    }
        
    
    public static boolean is_group(String name)
    {
        return customer_groups.containsKey(name); 
    }
    
    public static boolean add_group(String name, String terms, double discount)
    {
        customer_groups.put(name,new Group(name,terms,discount));
        return groupfile.save_changes(customer_groups);
    }
    
    public static boolean update_group(String name, String terms, double discount)
    {
        customer_groups.get(name).set_terms(terms);
        customer_groups.get(name).set_discount(discount);
        return groupfile.save_changes(customer_groups);
    }
    
    public static boolean delete_group(String name)
    {
        customer_groups.remove(name);
        return groupfile.save_changes(customer_groups);
    }
    
    public static String view_group(String name)
    {
        return customer_groups.get(name).printable();
    }
    
    public static boolean is_group_used(String name)
    {
        for (Customer member : customers)
        {
            if (member.get_group().equals(name))
            {
                return true;
            }
        }
        return false;
    }
    
    public static double get_group_discount(String id)
    {
        String group_name = find_customer(id).get_group();
        return customer_groups.get(group_name).get_discount();
    }
    
    public static void save_all_data()
    {
        if ((file.save_changes(customers) == groupfile.save_changes(customer_groups)) && (groupfile.save_changes(customer_groups)));
    }
    
}
